﻿using ControlzEx.Theming;
using LiveCharts;
using Microsoft.Win32;
using SmartDevice.Models;
using Stylet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SmartDevice.Pages
{
    public class MainShellViewModel : Screen
    {
        private readonly IWindowManager _windowManager;
        private readonly IViewFactory _viewFactory;
        private readonly IEventAggregator _events;

        public MainShellViewModel(IWindowManager windowManager, IViewFactory viewFactory, IEventAggregator events)
        {
            _windowManager = windowManager;
            _viewFactory = viewFactory;
            _events = events;
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            InitSettings();
            ReflashDatas();
        }

        #region 顶部工具条
        public bool IsSettingFlyoutOpen { get; set; }
        public void DoSetting()
        {
            IsSettingFlyoutOpen = !IsSettingFlyoutOpen;
        }
        public bool CanDoSetting => true;

        public void DoHelp()
        {
        }


        #endregion

        #region 设置

        /// <summary>
        /// 初始化设置
        /// </summary>
        private void InitSettings()
        {
            InitThemeSetting();
        }

        /// <summary>
        /// 打开设置页面
        /// </summary>
        public void SettingPageOpeningFinished()
        {
            //初始化配置          
        }

        /// <summary>
        /// 关闭设置页面
        /// </summary>
        public void SettingPageClosingFinished()
        {
            //保存相关配置
            Properties.Settings.Default.ThemeBackground = BackgroundName;
            Properties.Settings.Default.ThemeForeground = SelectedAccentColor.Name;
            Properties.Settings.Default.Save();
        }


        #region 主题

        private string BackgroundName;

        public bool ThemeBackgroundDarkChecked { get; set; }
        public bool ThemeBackgroundLightChecked { get; set; }

        public List<AccentColorData> AccentColors { get; set; }
        public AccentColorData SelectedAccentColor { get; set; }

        private void InitThemeSetting()
        {
            BackgroundName = Properties.Settings.Default.ThemeBackground;
            ThemeBackgroundDarkChecked = BackgroundName == "Dark";
            ThemeBackgroundLightChecked = !ThemeBackgroundDarkChecked;

            AccentColors = ThemeManager.Current.Themes
                                          .GroupBy(x => x.ColorScheme)
                                          .OrderBy(a => a.Key)
                                          .Select(a => new AccentColorData { Name = a.Key, ColorBrush = a.First().ShowcaseBrush })
                                          .ToList();

            SelectedAccentColor = new AccentColorData { Name = Properties.Settings.Default.ThemeForeground };

            this.Bind(s => SelectedAccentColor, (o, e) => AccentColorChanged());
        }

        public void ThemeBackgroundLightClick()
        {
            BackgroundName = "Light";
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        public void ThemeBackgroundDarkClick()
        {
            BackgroundName = "Dark";
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        public void AccentColorChanged()
        {
            ThemeManager.Current.ChangeTheme(Application.Current, $"{BackgroundName}.{SelectedAccentColor.Name}");
        }

        #endregion



        #endregion


        #region LiveCharts

        public string[] XTimeLabels { get; set; } = new string[] { "0" };
        public ChartValues<double> VoltageLineSeries { get; set; }
        public ChartValues<double> CurrentLineSeries { get; set; }

        public void ReflashDatas()
        {
            Random random = new Random();

            VoltageLineSeries = new ChartValues<double>();
            CurrentLineSeries = new ChartValues<double>();

            Task.Run(() =>
            {
                while (true)
                {
                    VoltageLineSeries.Add(random.Next(200, 300));
                    CurrentLineSeries.Add(random.Next(1, 20));

                    int Count = VoltageLineSeries.Count;
                    if (Count > 0)
                    {
                        XTimeLabels = new string[Count];
                        for (int i = 0; i < Count; i++)
                        {
                            XTimeLabels[i] = (i + 1).ToString();
                        }
                    }

                    Thread.Sleep(2000);
                }
            });
        }

        #endregion
    }
}
